package com.daluzy.chmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.bean.Area;
import com.daluzy.chmanager.bean.Employee;
import com.daluzy.chmanager.bean.Manager;
import com.daluzy.chmanager.bean.School;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class EmployeeInfoActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_employee_name)
    TextView tvEmployeeName;
    @BindView(R.id.tv_employee_phone)
    TextView tvEmployeePhone;
    @BindView(R.id.tv_employee_school)
    TextView tvEmployeeSchool;
    @BindView(R.id.tv_employee_area)
    TextView tvEmployeeArea;
    @BindView(R.id.tv_employee_sex)
    TextView tvEmployeeSex;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.ll_sex)
    LinearLayout llSex;
    private Employee employee;
    private Manager manager;
    private School school;
    private String employeeId;
    int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employee_info_daluzy);
        ButterKnife.bind(this);
        type = MyApplication.getIntance().getUserType();

        if (type == 2) {
            employeeId = getIntent().getStringExtra("employeeId");
        } else {
            manager = MyApplication.getIntance().getManager();
        }

    }

    private void initData() {
        employeeId = MyApplication.getIntance().getEmployee().getObjectId();
        BmobQuery<Employee> bmobQuery = new BmobQuery<Employee>();
        bmobQuery.getObject(employeeId, new QueryListener<Employee>() {
            @Override
            public void done(Employee object, BmobException e) {
                if (e == null) {
                    MyApplication.getIntance().setEmployee(object);
                    setData(object);
                } else {
                    Toast.makeText(EmployeeInfoActivity.this, "查询失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initData2() {
        BmobQuery<Manager> bmobQuery = new BmobQuery<Manager>();
        bmobQuery.getObject(manager.getObjectId(), new QueryListener<Manager>() {
            @Override
            public void done(Manager object, BmobException e) {
                if (e == null) {
                    MyApplication.getIntance().setManager(object);
                    setData2(object);
                } else {
                    Toast.makeText(EmployeeInfoActivity.this, "查询失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                if (type == 2) {
                    Intent intent = new Intent(EmployeeInfoActivity.this, AddEmployeeActivity.class);
                    intent.putExtra("employee", employee);
                    intent.putExtra("type", 1);
                    startActivity(intent);
                } else {
                    Toast.makeText(EmployeeInfoActivity.this, "管理员无法编辑自己的信息，请联系后台人员", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    private void setData(Employee employee) {
        this.employee = employee;
        tvEmployeeArea.setText(employee.getEmployee_area());
        tvEmployeeName.setText(employee.getEmployee_name());
        tvEmployeePhone.setText(employee.getEmployee_phone());
        tvEmployeeSex.setText(employee.getEmployee_sex());

        initData(employee.getEmployee_area());
    }

    private void setData2(Manager manager) {
        this.manager = manager;
        llArea.setVisibility(View.GONE);
        tvEmployeeName.setText(manager.getManager_name());
        tvEmployeePhone.setText(manager.getManager_phone());
        llSex.setVisibility(View.GONE);
        tvEmployeeSchool.setText(manager.getSchool_name());
    }

    private void initData(String areaname) {

        BmobQuery<Area> query = new BmobQuery<>();
        query.addWhereEqualTo("area_name", areaname);
        query.findObjects(new FindListener<Area>() {
            @Override
            public void done(List<Area> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size() > 0)
                        tvEmployeeSchool.setText(object.get(0).getArea_school());
                }
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (type == 2)
            initData();
        else initData2();
    }
}
